/****************************************************/
*Fairness: Data Cleaning							*/
*Date Created: Jan 29, 2017							*/
*Notes: Read in and clean data for part 2 of 		*/
*		fairness experiment							*/
/****************************************************/

clear all
set more off

/****************************************/
/*				PREAMBLE	 			*/
/****************************************/

*Set directories: Change local directory paths
global raw_dir "<INSERT RAW DATA DIRECTORY HERE>"
global out_dir "<INSERT OUTPUT DIRECTORY HERE>"
cd "$raw_dir/src"

/****************************************/
/*		READ IN DATA: PART 2			*/
/****************************************/
*Import xlsx files
local filenames 120509_1658_NK 120509_1855_BK 120514_1712_NK 120516_1646_SK ///

foreach excelfile in `filenames' {
				
import excel using `excelfile'.xlsx 

*rename
rename A sessionfile
rename B part
rename C datatype
rename D period
rename E subjectnum
rename F group
rename G profit
rename H totalprofit
rename I participate
rename J role
rename K TimeOKPart2IntroductionOK
rename L counter
rename M guessperiod
rename N b
rename O b1
rename P oldvalue
rename Q oldaccept
rename R oldoffer
rename S oldcost
rename T probBDM
rename U TimeOKPart2Buyers1OK
rename V roundn
rename W TimeOKPart2Sellers1351OK
rename X idealX90
rename Y TimeOKPart2Sellers2490OK
rename Z idealX90m1
rename AA idealX90m2
rename AB idealX90p1
rename AC idealX90p2
rename AD Xm190X_200
rename AE Xm190X_150
rename AF Xm190X_100
rename AG Xm190X_075
rename AH Xm190X_050
rename AI Xm190X_025
rename AJ Xm190
rename AK Xm190Xo_025
rename AL Xm190Xo_050
rename AM Xm190Xo_075
rename AN Xm190Xo_100
rename AO Xm190Xo_150
rename AP Xm190Xo_200
rename AQ TimeOKPart2Sellers2490Xm1OK
rename AR Xm290X_200
rename AS Xm290X_150
rename AT Xm290X_100
rename AU Xm290X_075
rename AV Xm290X_050
rename AW Xm290X_025
rename AX Xm290
rename AY Xm290Xo_025
rename AZ Xm290Xo_050
rename BA Xm290Xo_075
rename BB Xm290Xo_100
rename BC Xm290Xo_150
rename BD Xm290Xo_200
rename BE TimeOKPart2Sellers2490Xm2OK
rename BF Xp190X_200
rename BG Xp190X_150
rename BH Xp190X_100
rename BI Xp190X_075
rename BJ Xp190X_050
rename BK Xp190X_025
rename BL Xp190
rename BM Xp190Xo_025
rename BN Xp190Xo_050
rename BO Xp190Xo_075
rename BP Xp190Xo_100
rename BQ Xp190Xo_150
rename BR Xp190Xo_200
rename BS TimeOKPart2Sellers2490Xp1OK
rename BT Xp290X_200
rename BU Xp290X_150
rename BV Xp290X_100
rename BW Xp290X_075
rename BX Xp290X_050
rename BY Xp290X_025
rename BZ Xp290
rename CA Xp290Xo_025
rename CB Xp290Xo_050
rename CC Xp290Xo_075
rename CD Xp290Xo_100
rename CE Xp290Xo_150
rename CF Xp290Xo_200
rename CG TimeOKPart2Sellers2490Xp2OK
rename CH idealX70
rename CI TimeOKPart2Sellers2470OK
rename CJ idealX70m1
rename CK idealX70m2
rename CL idealX70p1
rename CM idealX70p2
rename CN Xm170X_200
rename CO Xm170X_150
rename CP Xm170X_100
rename CQ Xm170X_075
rename CR Xm170X_050
rename CS Xm170X_025
rename CT Xm170
rename CU Xm170Xo_025
rename CV Xm170Xo_050
rename CW Xm170Xo_075
rename CX Xm170Xo_100
rename CY Xm170Xo_150
rename CZ Xm170Xo_200
rename DA TimeOKPart2Sellers2470Xm1OK
rename DB Xm270X_200
rename DC Xm270X_150
rename DD Xm270X_100
rename DE Xm270X_075
rename DF Xm270X_050
rename DG Xm270X_025
rename DH Xm270
rename DI Xm270Xo_025
rename DJ Xm270Xo_050
rename DK Xm270Xo_075
rename DL Xm270Xo_100
rename DM Xm270Xo_150
rename DN Xm270Xo_200
rename DO TimeOKPart2Sellers2470Xm2OK
rename DP Xp170X_200
rename DQ Xp170X_150
rename DR Xp170X_100
rename DS Xp170X_075
rename DT Xp170X_050
rename DU Xp170X_025
rename DV Xp170
rename DW Xp170Xo_025
rename DX Xp170Xo_050
rename DY Xp170Xo_075
rename DZ Xp170Xo_100
rename EA Xp170Xo_150
rename EB Xp170Xo_200
rename EC TimeOKPart2Sellers2470Xp1OK
rename ED Xp270X_200
rename EE Xp270X_150
rename EF Xp270X_100
rename EG Xp270X_075
rename EH Xp270X_050
rename EI Xp270X_025
rename EJ Xp270
rename EK Xp270Xo_025
rename EL Xp270Xo_050
rename EM Xp270Xo_075
rename EN Xp270Xo_100
rename EO Xp270Xo_150
rename EP Xp270Xo_200
rename EQ TimeOKPart2Sellers2470Xp2OK

keep if part==2
keep if datatype=="subjects"
drop if period=="Period"
drop Time* profit totalprofit participate datatype participate counter b b1 roundn

*add labels:
label variable sessionfile "Session file number"
label variable part "Experiment part; 4 parts total"
label variable period "Period; 10 periods total in part 2"
label variable subjectnum "zTree subject number"
label variable group "zTree group identifies pairs"
label variable role "Role of subject; Buyer==1, Seller==2"
label variable guessperiod "Period from Part 1 current player is guessing"
label variable oldvalue "Value of Buyer from Part 1; Randomly selected 70 or 90"
label variable oldaccept "Seller accepted offer from Part 1; oldaccepted==1, otherwise 0"
label variable oldoffer "Offer from Part 1"
label variable oldcost "Cost of Seller from Part 1; Randomly selected 10 or 30"
label variable probBDM "Buyer's guess of probability seller accepted offer; Seller's guess of probability buyer has value==70"
label variable idealX90 "Ideal X90 of sellers in treatment 2 and 4"
label variable idealX90m1 "Ideal X90-1 of sellers in treatment 2 and 4"
label variable idealX90m2 "Ideal X90-5 of sellers in treatment 2 and 4"
label variable idealX90p1 "Ideal X90+1 of sellers in treatment 2 and 4"
label variable idealX90p2 "Ideal X90-5 of sellers in treatment 2 and 4"
label variable Xm190X_200 "Choose which one prefer more: idealX90 and 200 cents==1 (left) or idealX90m1==2 (right)"
label variable Xm190X_150 "Choose which one prefer more: idealX90 and 150 cents==1 (left) or idealX90m1==2 (right)"
label variable Xm190X_100 "Choose which one prefer more: idealX90 and 100 cents==1 (left) or idealX90m1==2 (right)"
label variable Xm190X_075 "Choose which one prefer more: idealX90 and 75 cents==1 (left) or idealX90m1==2 (right)"
label variable Xm190X_050 "Choose which one prefer more: idealX90 and 50 cents==1 (left) or idealX90m1==2 (right)"
label variable Xm190X_025 "Choose which one prefer more: idealX90 and 25 cents==1 (left) or idealX90m1==2 (right)"
label variable Xm190 "Choose which one prefer more: idealX90==1 (left) or idealX90m1==2 (right)"
label variable Xm190Xo_025 "Choose which one prefer more: idealX90==1 (left) or idealX90m1 and 25 cents==2 (right)"
label variable Xm190Xo_050 "Choose which one prefer more: idealX90==1 (left) or idealX90m1 and 50 cents==2 (right)"
label variable Xm190Xo_075 "Choose which one prefer more: idealX90==1 (left) or idealX90m1 and 75 cents==2 (right)"
label variable Xm190Xo_100 "Choose which one prefer more: idealX90==1 (left) or idealX90m1 and 100 cents==2 (right)"
label variable Xm190Xo_150 "Choose which one prefer more: idealX90==1 (left) or idealX90m1 and 150 cents==2 (right)"
label variable Xm190Xo_200 "Choose which one prefer more: idealX90==1 (left) or idealX90m1 and 200 cents==2 (right)"
label variable Xm290X_200 "Choose which one prefer more: idealX90 and 200 cents==1 (left) or idealX90m2==2 (right)"
label variable Xm290X_150 "Choose which one prefer more: idealX90 and 150 cents==1 (left) or idealX90m2==2 (right)"
label variable Xm290X_100 "Choose which one prefer more: idealX90 and 100 cents==1 (left) or idealX90m2==2 (right)"
label variable Xm290X_075 "Choose which one prefer more: idealX90 and 75 cents==1 (left) or idealX90m2==2 (right)"
label variable Xm290X_050 "Choose which one prefer more: idealX90 and 50 cents==1 (left) or idealX90m2==2 (right)"
label variable Xm290X_025 "Choose which one prefer more: idealX90 and 25 cents==1 (left) or idealX90m2==2 (right)"
label variable Xm290 "Choose which one prefer more: idealX90==1 (left) or idealX90m2==2 (right)"
label variable Xm290Xo_025 "Choose which one prefer more: idealX90==1 (left) or idealX90m2 and 25 cents==2 (right)"
label variable Xm290Xo_050 "Choose which one prefer more: idealX90==1 (left) or idealX90m2 and 50 cents==2 (right)"
label variable Xm290Xo_075 "Choose which one prefer more: idealX90==1 (left) or idealX90m2 and 75 cents==2 (right)"
label variable Xm290Xo_100 "Choose which one prefer more: idealX90==1 (left) or idealX90m2 and 100 cents==2 (right)"
label variable Xm290Xo_150 "Choose which one prefer more: idealX90==1 (left) or idealX90m2 and 150 cents==2 (right)"
label variable Xm290Xo_200 "Choose which one prefer more: idealX90==1 (left) or idealX90m2 and 200 cents==2 (right)"
label variable Xp190X_200 "Choose which one prefer more: idealX90 and 200 cents==1 (left) or idealX90p1==2 (right)"
label variable Xp190X_150 "Choose which one prefer more: idealX90 and 150 cents==1 (left) or idealX90p1==2 (right)"
label variable Xp190X_100 "Choose which one prefer more: idealX90 and 100 cents==1 (left) or idealX90p1==2 (right)"
label variable Xp190X_075 "Choose which one prefer more: idealX90 and 75 cents==1 (left) or idealX90p1==2 (right)"
label variable Xp190X_050 "Choose which one prefer more: idealX90 and 50 cents==1 (left) or idealX90p1==2 (right)"
label variable Xp190X_025 "Choose which one prefer more: idealX90 and 25 cents==1 (left) or idealX90p1==2 (right)"
label variable Xp190 "Choose which one prefer more: idealX90==1 (left) or idealX90p1==2 (right)"
label variable Xp190Xo_025 "Choose which one prefer more: idealX90==1 (left) or idealX90p1 and 25 cents==2 (right)"
label variable Xp190Xo_050 "Choose which one prefer more: idealX90==1 (left) or idealX90p1 and 50 cents==2 (right)"
label variable Xp190Xo_075 "Choose which one prefer more: idealX90==1 (left) or idealX90p1 and 75 cents==2 (right)"
label variable Xp190Xo_100 "Choose which one prefer more: idealX90==1 (left) or idealX90p1 and 100 cents==2 (right)"
label variable Xp190Xo_150 "Choose which one prefer more: idealX90==1 (left) or idealX90p1 and 150 cents==2 (right)"
label variable Xp190Xo_200 "Choose which one prefer more: idealX90==1 (left) or idealX90p1 and 200 cents==2 (right)"
label variable Xp290X_200 "Choose which one prefer more: idealX90 and 200 cents==1 (left) or idealX90p2==2 (right)"
label variable Xp290X_150 "Choose which one prefer more: idealX90 and 150 cents==1 (left) or idealX90p2==2 (right)"
label variable Xp290X_100 "Choose which one prefer more: idealX90 and 100 cents==1 (left) or idealX90p2==2 (right)"
label variable Xp290X_075 "Choose which one prefer more: idealX90 and 75 cents==1 (left) or idealX90p2==2 (right)"
label variable Xp290X_050 "Choose which one prefer more: idealX90 and 50 cents==1 (left) or idealX90p2==2 (right)"
label variable Xp290X_025 "Choose which one prefer more: idealX90 and 25 cents==1 (left) or idealX90p2==2 (right)"
label variable Xp290 "Choose which one prefer more: idealX90==1 (left) or idealX90p2==2 (right)"
label variable Xp290Xo_025 "Choose which one prefer more: idealX90==1 (left) or idealX90p2 and 25 cents==2 (right)"
label variable Xp290Xo_050 "Choose which one prefer more: idealX90==1 (left) or idealX90p2 and 50 cents==2 (right)"
label variable Xp290Xo_075 "Choose which one prefer more: idealX90==1 (left) or idealX90p2 and 75 cents==2 (right)"
label variable Xp290Xo_100 "Choose which one prefer more: idealX90==1 (left) or idealX90p2 and 100 cents==2 (right)"
label variable Xp290Xo_150 "Choose which one prefer more: idealX90==1 (left) or idealX90p2 and 150 cents==2 (right)"
label variable Xp290Xo_200 "Choose which one prefer more: idealX90==1 (left) or idealX90p2 and 200 cents==2 (right)"
label variable idealX70 "Ideal X70 of sellers in treatment 2 and 4"
label variable idealX70m1 "Ideal X70-1 of sellers in treatment 2 and 4"
label variable idealX70m2 "Ideal X70-5 of sellers in treatment 2 and 4"
label variable idealX70p1 "Ideal X70+1 of sellers in treatment 2 and 4"
label variable idealX70p2 "Ideal X70-5 of sellers in treatment 2 and 4"
label variable Xm170X_200 "Choose which one prefer more: idealX70 and 200 cents==1 (left) or idealX70m1==2 (right)"
label variable Xm170X_150 "Choose which one prefer more: idealX70 and 150 cents==1 (left) or idealX70m1==2 (right)"
label variable Xm170X_100 "Choose which one prefer more: idealX70 and 100 cents==1 (left) or idealX70m1==2 (right)"
label variable Xm170X_075 "Choose which one prefer more: idealX70 and 75 cents==1 (left) or idealX70m1==2 (right)"
label variable Xm170X_050 "Choose which one prefer more: idealX70 and 50 cents==1 (left) or idealX70m1==2 (right)"
label variable Xm170X_025 "Choose which one prefer more: idealX70 and 25 cents==1 (left) or idealX70m1==2 (right)"
label variable Xm170 "Choose which one prefer more: idealX70==1 (left) or idealX70m1==2 (right)"
label variable Xm170Xo_025 "Choose which one prefer more: idealX70==1 (left) or idealX70m1 and 25 cents==2 (right)"
label variable Xm170Xo_050 "Choose which one prefer more: idealX70==1 (left) or idealX70m1 and 50 cents==2 (right)"
label variable Xm170Xo_075 "Choose which one prefer more: idealX70==1 (left) or idealX70m1 and 75 cents==2 (right)"
label variable Xm170Xo_100 "Choose which one prefer more: idealX70==1 (left) or idealX70m1 and 100 cents==2 (right)"
label variable Xm170Xo_150 "Choose which one prefer more: idealX70==1 (left) or idealX70m1 and 150 cents==2 (right)"
label variable Xm170Xo_200 "Choose which one prefer more: idealX70==1 (left) or idealX70m1 and 200 cents==2 (right)"
label variable Xm270X_200 "Choose which one prefer more: idealX70 and 200 cents==1 (left) or idealX70m2==2 (right)"
label variable Xm270X_150 "Choose which one prefer more: idealX70 and 150 cents==1 (left) or idealX70m2==2 (right)"
label variable Xm270X_100 "Choose which one prefer more: idealX70 and 100 cents==1 (left) or idealX70m2==2 (right)"
label variable Xm270X_075 "Choose which one prefer more: idealX70 and 75 cents==1 (left) or idealX70m2==2 (right)"
label variable Xm270X_050 "Choose which one prefer more: idealX70 and 50 cents==1 (left) or idealX70m2==2 (right)"
label variable Xm270X_025 "Choose which one prefer more: idealX70 and 25 cents==1 (left) or idealX70m2==2 (right)"
label variable Xm270 "Choose which one prefer more: idealX70==1 (left) or idealX70m2==2 (right)"
label variable Xm270Xo_025 "Choose which one prefer more: idealX70==1 (left) or idealX70m2 and 25 cents==2 (right)"
label variable Xm270Xo_050 "Choose which one prefer more: idealX70==1 (left) or idealX70m2 and 50 cents==2 (right)"
label variable Xm270Xo_075 "Choose which one prefer more: idealX70==1 (left) or idealX70m2 and 75 cents==2 (right)"
label variable Xm270Xo_100 "Choose which one prefer more: idealX70==1 (left) or idealX70m2 and 100 cents==2 (right)"
label variable Xm270Xo_150 "Choose which one prefer more: idealX70==1 (left) or idealX70m2 and 150 cents==2 (right)"
label variable Xm270Xo_200 "Choose which one prefer more: idealX70==1 (left) or idealX70m2 and 200 cents==2 (right)"
label variable Xp170X_200 "Choose which one prefer more: idealX70 and 200 cents==1 (left) or idealX70p1==2 (right)"
label variable Xp170X_150 "Choose which one prefer more: idealX70 and 150 cents==1 (left) or idealX70p1==2 (right)"
label variable Xp170X_100 "Choose which one prefer more: idealX70 and 100 cents==1 (left) or idealX70p1==2 (right)"
label variable Xp170X_075 "Choose which one prefer more: idealX70 and 75 cents==1 (left) or idealX70p1==2 (right)"
label variable Xp170X_050 "Choose which one prefer more: idealX70 and 50 cents==1 (left) or idealX70p1==2 (right)"
label variable Xp170X_025 "Choose which one prefer more: idealX70 and 25 cents==1 (left) or idealX70p1==2 (right)"
label variable Xp170 "Choose which one prefer more: idealX70==1 (left) or idealX70p1==2 (right)"
label variable Xp170Xo_025 "Choose which one prefer more: idealX70==1 (left) or idealX70p1 and 25 cents==2 (right)"
label variable Xp170Xo_050 "Choose which one prefer more: idealX70==1 (left) or idealX70p1 and 50 cents==2 (right)"
label variable Xp170Xo_075 "Choose which one prefer more: idealX70==1 (left) or idealX70p1 and 75 cents==2 (right)"
label variable Xp170Xo_100 "Choose which one prefer more: idealX70==1 (left) or idealX70p1 and 100 cents==2 (right)"
label variable Xp170Xo_150 "Choose which one prefer more: idealX70==1 (left) or idealX70p1 and 150 cents==2 (right)"
label variable Xp170Xo_200 "Choose which one prefer more: idealX70==1 (left) or idealX70p1 and 200 cents==2 (right)"
label variable Xp270X_200 "Choose which one prefer more: idealX70 and 200 cents==1 (left) or idealX70p2==2 (right)"
label variable Xp270X_150 "Choose which one prefer more: idealX70 and 150 cents==1 (left) or idealX70p2==2 (right)"
label variable Xp270X_100 "Choose which one prefer more: idealX70 and 100 cents==1 (left) or idealX70p2==2 (right)"
label variable Xp270X_075 "Choose which one prefer more: idealX70 and 75 cents==1 (left) or idealX70p2==2 (right)"
label variable Xp270X_050 "Choose which one prefer more: idealX70 and 50 cents==1 (left) or idealX70p2==2 (right)"
label variable Xp270X_025 "Choose which one prefer more: idealX70 and 25 cents==1 (left) or idealX70p2==2 (right)"
label variable Xp270 "Choose which one prefer more: idealX70==1 (left) or idealX70p2==2 (right)"
label variable Xp270Xo_025 "Choose which one prefer more: idealX70==1 (left) or idealX70p2 and 25 cents==2 (right)"
label variable Xp270Xo_050 "Choose which one prefer more: idealX70==1 (left) or idealX70p2 and 50 cents==2 (right)"
label variable Xp270Xo_075 "Choose which one prefer more: idealX70==1 (left) or idealX70p2 and 75 cents==2 (right)"
label variable Xp270Xo_100 "Choose which one prefer more: idealX70==1 (left) or idealX70p2 and 100 cents==2 (right)"
label variable Xp270Xo_150 "Choose which one prefer more: idealX70==1 (left) or idealX70p2 and 150 cents==2 (right)"
label variable Xp270Xo_200 "Choose which one prefer more: idealX70==1 (left) or idealX70p2 and 200 cents==2 (right)"

tostring sessionfile-Xp270Xo_200, replace

local varlistostring probBDM idealX90 idealX90m1 idealX90m2 idealX90p1 idealX90p2 ///
Xm190X_200 Xm190X_150 Xm190X_100 Xm190X_075 Xm190X_050 Xm190X_025 ///
Xm190 Xm190Xo_025 Xm190Xo_050 Xm190Xo_075 Xm190Xo_100 Xm190Xo_150 ///
Xm190Xo_200 Xm290X_200 Xm290X_150 Xm290X_100 Xm290X_075 Xm290X_050 ///
Xm290X_025 Xm290 Xm290Xo_025 Xm290Xo_050 Xm290Xo_075 Xm290Xo_100 /// 
Xm290Xo_150 Xm290Xo_200 Xp190X_200 Xp190X_150 Xp190X_100 Xp190X_075 /// 
Xp190X_050 Xp190X_025 Xp190 Xp190Xo_025 Xp190Xo_050 Xp190Xo_075 /// 
Xp190Xo_100 Xp190Xo_150 Xp190Xo_200 Xp290X_200 Xp290X_150 Xp290X_100 /// 
Xp290X_075 Xp290X_050 Xp290X_025 Xp290 Xp290Xo_025 Xp290Xo_050 /// 
Xp290Xo_075 Xp290Xo_100 Xp290Xo_150 Xp290Xo_200 ///
idealX70 idealX70m1 idealX70m2 idealX70p1 idealX70p2 /// 
Xm170X_200 Xm170X_150 Xm170X_100 Xm170X_075 Xm170X_050 Xm170X_025 /// 
Xm170 Xm170Xo_025 Xm170Xo_050 Xm170Xo_075 Xm170Xo_100 Xm170Xo_150 /// 
Xm170Xo_200 Xm270X_200 Xm270X_150 Xm270X_100 Xm270X_075 Xm270X_050 /// 
Xm270X_025 Xm270 Xm270Xo_025 Xm270Xo_050 Xm270Xo_075 Xm270Xo_100 /// 
Xm270Xo_150 Xm270Xo_200 Xp170X_200 Xp170X_150 Xp170X_100 Xp170X_075 /// 
Xp170X_050 Xp170X_025 Xp170 Xp170Xo_025 Xp170Xo_050 Xp170Xo_075 ///
Xp170Xo_100 Xp170Xo_150 Xp170Xo_200 Xp270X_200 Xp270X_150 Xp270X_100 /// 
Xp270X_075 Xp270X_050 Xp270X_025 Xp270 Xp270Xo_025 Xp270Xo_050 /// 
Xp270Xo_075 Xp270Xo_100 Xp270Xo_150 Xp270Xo_200 ///

foreach varname in `varlistostring' {	
		replace `varname' = "" if `varname' == "-"
		destring `varname', replace
}


destring sessionfile-Xp270Xo_200, replace

save "$out_dir/`excelfile'_part2", replace 
clear 
}


local filenames 120522_1938_CI 120524_1736_BK 120529_1820_SK 120604_1909_CI ///
				120614_1120 120614_1248 120614_1356 120615_1109 120615_1234 ///
				120615_1419 120618_1149 120618_1350 120619_1130 120619_1343 ///
				120620_1136 120620_1345

foreach excelfile in `filenames' {
				
import excel using `excelfile'.xlsx 

*rename
rename A sessionfile
rename B part
rename C datatype
rename D period
rename E subjectnum
rename F group
rename G profit
rename H totalprofit
rename I participate
rename J role
rename K TimeOKPart2IntroductionOK
rename L counter
rename M guessperiod
rename N b
rename O b1
rename P oldvalue
rename Q oldaccept
rename R oldoffer
rename S oldcost
rename T probBDM
rename U TimeOKPart2Buyers1OK
rename V roundn
rename W TimeOKPart2Sellers1351OK
rename X idealX90
rename Y TimeOKPart2Sellers2490OK
rename Z idealX90m1
rename AA idealX90m2
rename AB idealX90p1
rename AC idealX90p2
rename AD Xm190X_100
rename AE Xm190X_050
rename AF Xm190X_025
rename AG Xm190
rename AH Xm190Xo_025
rename AI Xm190Xo_050
rename AJ Xm190Xo_100
rename AK TimeOKPart2Sellers2490Xm1OK
rename AL Xm290X_100
rename AM Xm290X_050
rename AN Xm290X_025
rename AO Xm290
rename AP Xm290Xo_025
rename AQ Xm290Xo_050
rename AR Xm290Xo_100
rename AS TimeOKPart2Sellers2490Xm2OK
rename AT Xp190X_100
rename AU Xp190X_050
rename AV Xp190X_025
rename AW Xp190
rename AX Xp190Xo_025
rename AY Xp190Xo_050
rename AZ Xp190Xo_100
rename BA TimeOKPart2Sellers2490Xp1OK
rename BB Xp290X_100
rename BC Xp290X_050
rename BD Xp290X_025
rename BE Xp290
rename BF Xp290Xo_025
rename BG Xp290Xo_050
rename BH Xp290Xo_100
rename BI TimeOKPart2Sellers2490Xp2OK
rename BJ idealX70
rename BK TimeOKPart2Sellers2470OK
rename BL idealX70m1
rename BM idealX70m2
rename BN idealX70p1
rename BO idealX70p2
rename BP Xm170X_100
rename BQ Xm170X_050
rename BR Xm170X_025
rename BS Xm170
rename BT Xm170Xo_025
rename BU Xm170Xo_050
rename BV Xm170Xo_100
rename BW TimeOKPart2Sellers2470Xm1OK
rename BX Xm270X_100
rename BY Xm270X_050
rename BZ Xm270X_025
rename CA Xm270
rename CB Xm270Xo_025
rename CC Xm270Xo_050
rename CD Xm270Xo_100
rename CE TimeOKPart2Sellers2470Xm2OK
rename CF Xp170X_100
rename CG Xp170X_050
rename CH Xp170X_025
rename CI Xp170
rename CJ Xp170Xo_025
rename CK Xp170Xo_050
rename CL Xp170Xo_100
rename CM TimeOKPart2Sellers2470Xp1OK
rename CN Xp270X_100
rename CO Xp270X_050
rename CP Xp270X_025
rename CQ Xp270
rename CR Xp270Xo_025
rename CS Xp270Xo_050
rename CT Xp270Xo_100
rename CU TimeOKPart2Sellers2470Xp2OK

keep if part==2
keep if datatype=="subjects"
drop if period=="Period"
drop Time* profit totalprofit participate datatype participate counter b b1 roundn

*add labels:
label variable sessionfile "Session file number"
label variable part "Experiment part; 4 parts total"
label variable period "Period; 10 periods total in part 2"
label variable subjectnum "zTree subject number"
label variable group "zTree group identifies pairs"
label variable role "Role of subject; Buyer==1, Seller==2"
label variable guessperiod "Period from Part 1 current player is guessing"
label variable oldvalue "Value of Buyer from Part 1; Randomly selected 70 or 90"
label variable oldaccept "Seller accepted offer from Part 1; oldaccepted==1, otherwise 0"
label variable oldoffer "Offer from Part 1"
label variable oldcost "Cost of Seller from Part 1; Randomly selected 10 or 30"
label variable probBDM "Buyer's guess of probability seller accepted offer; Seller's guess of probability buyer has value==70"
label variable idealX90 "Ideal X90 of sellers in treatment 2 and 4"
label variable idealX90m1 "Ideal X90-1 of sellers in treatment 2 and 4"
label variable idealX90m2 "Ideal X90-5 of sellers in treatment 2 and 4"
label variable idealX90p1 "Ideal X90+1 of sellers in treatment 2 and 4"
label variable idealX90p2 "Ideal X90-5 of sellers in treatment 2 and 4"
label variable Xm190X_100 "Choose which one prefer more: idealX90 and 100 cents==1 (left) or idealX90m1==2 (right)"
label variable Xm190X_050 "Choose which one prefer more: idealX90 and 50 cents==1 (left) or idealX90m1==2 (right)"
label variable Xm190X_025 "Choose which one prefer more: idealX90 and 25 cents==1 (left) or idealX90m1==2 (right)"
label variable Xm190 "Choose which one prefer more: idealX90==1 (left) or idealX90m1==2 (right)"
label variable Xm190Xo_025 "Choose which one prefer more: idealX90==1 (left) or idealX90m1 and 25 cents==2 (right)"
label variable Xm190Xo_050 "Choose which one prefer more: idealX90==1 (left) or idealX90m1 and 50 cents==2 (right)"
label variable Xm190Xo_100 "Choose which one prefer more: idealX90==1 (left) or idealX90m1 and 100 cents==2 (right)"
label variable Xm290X_100 "Choose which one prefer more: idealX90 and 100 cents==1 (left) or idealX90m2==2 (right)"
label variable Xm290X_050 "Choose which one prefer more: idealX90 and 50 cents==1 (left) or idealX90m2==2 (right)"
label variable Xm290X_025 "Choose which one prefer more: idealX90 and 25 cents==1 (left) or idealX90m2==2 (right)"
label variable Xm290 "Choose which one prefer more: idealX90==1 (left) or idealX90m2==2 (right)"
label variable Xm290Xo_025 "Choose which one prefer more: idealX90==1 (left) or idealX90m2 and 25 cents==2 (right)"
label variable Xm290Xo_050 "Choose which one prefer more: idealX90==1 (left) or idealX90m2 and 50 cents==2 (right)"
label variable Xm290Xo_100 "Choose which one prefer more: idealX90==1 (left) or idealX90m2 and 100 cents==2 (right)"
label variable Xp190X_100 "Choose which one prefer more: idealX90 and 100 cents==1 (left) or idealX90p1==2 (right)"
label variable Xp190X_050 "Choose which one prefer more: idealX90 and 50 cents==1 (left) or idealX90p1==2 (right)"
label variable Xp190X_025 "Choose which one prefer more: idealX90 and 25 cents==1 (left) or idealX90p1==2 (right)"
label variable Xp190 "Choose which one prefer more: idealX90==1 (left) or idealX90p1==2 (right)"
label variable Xp190Xo_025 "Choose which one prefer more: idealX90==1 (left) or idealX90p1 and 25 cents==2 (right)"
label variable Xp190Xo_050 "Choose which one prefer more: idealX90==1 (left) or idealX90p1 and 50 cents==2 (right)"
label variable Xp190Xo_100 "Choose which one prefer more: idealX90==1 (left) or idealX90p1 and 100 cents==2 (right)"
label variable Xp290X_100 "Choose which one prefer more: idealX90 and 100 cents==1 (left) or idealX90p2==2 (right)"
label variable Xp290X_050 "Choose which one prefer more: idealX90 and 50 cents==1 (left) or idealX90p2==2 (right)"
label variable Xp290X_025 "Choose which one prefer more: idealX90 and 25 cents==1 (left) or idealX90p2==2 (right)"
label variable Xp290 "Choose which one prefer more: idealX90==1 (left) or idealX90p2==2 (right)"
label variable Xp290Xo_025 "Choose which one prefer more: idealX90==1 (left) or idealX90p2 and 25 cents==2 (right)"
label variable Xp290Xo_050 "Choose which one prefer more: idealX90==1 (left) or idealX90p2 and 50 cents==2 (right)"
label variable Xp290Xo_100 "Choose which one prefer more: idealX90==1 (left) or idealX90p2 and 100 cents==2 (right)"
label variable idealX70 "Ideal X70 of sellers in treatment 2 and 4"
label variable idealX70m1 "Ideal X70-1 of sellers in treatment 2 and 4"
label variable idealX70m2 "Ideal X70-5 of sellers in treatment 2 and 4"
label variable idealX70p1 "Ideal X70+1 of sellers in treatment 2 and 4"
label variable idealX70p2 "Ideal X70-5 of sellers in treatment 2 and 4"
label variable Xm170X_100 "Choose which one prefer more: idealX70 and 100 cents==1 (left) or idealX70m1==2 (right)"
label variable Xm170X_050 "Choose which one prefer more: idealX70 and 50 cents==1 (left) or idealX70m1==2 (right)"
label variable Xm170X_025 "Choose which one prefer more: idealX70 and 25 cents==1 (left) or idealX70m1==2 (right)"
label variable Xm170 "Choose which one prefer more: idealX70==1 (left) or idealX70m1==2 (right)"
label variable Xm170Xo_025 "Choose which one prefer more: idealX70==1 (left) or idealX70m1 and 25 cents==2 (right)"
label variable Xm170Xo_050 "Choose which one prefer more: idealX70==1 (left) or idealX70m1 and 50 cents==2 (right)"
label variable Xm170Xo_100 "Choose which one prefer more: idealX70==1 (left) or idealX70m1 and 100 cents==2 (right)"
label variable Xm270X_100 "Choose which one prefer more: idealX70 and 100 cents==1 (left) or idealX70m2==2 (right)"
label variable Xm270X_050 "Choose which one prefer more: idealX70 and 50 cents==1 (left) or idealX70m2==2 (right)"
label variable Xm270X_025 "Choose which one prefer more: idealX70 and 25 cents==1 (left) or idealX70m2==2 (right)"
label variable Xm270 "Choose which one prefer more: idealX70==1 (left) or idealX70m2==2 (right)"
label variable Xm270Xo_025 "Choose which one prefer more: idealX70==1 (left) or idealX70m2 and 25 cents==2 (right)"
label variable Xm270Xo_050 "Choose which one prefer more: idealX70==1 (left) or idealX70m2 and 50 cents==2 (right)"
label variable Xm270Xo_100 "Choose which one prefer more: idealX70==1 (left) or idealX70m2 and 100 cents==2 (right)"
label variable Xp170X_100 "Choose which one prefer more: idealX70 and 100 cents==1 (left) or idealX70p1==2 (right)"
label variable Xp170X_050 "Choose which one prefer more: idealX70 and 50 cents==1 (left) or idealX70p1==2 (right)"
label variable Xp170X_025 "Choose which one prefer more: idealX70 and 25 cents==1 (left) or idealX70p1==2 (right)"
label variable Xp170 "Choose which one prefer more: idealX70==1 (left) or idealX70p1==2 (right)"
label variable Xp170Xo_025 "Choose which one prefer more: idealX70==1 (left) or idealX70p1 and 25 cents==2 (right)"
label variable Xp170Xo_050 "Choose which one prefer more: idealX70==1 (left) or idealX70p1 and 50 cents==2 (right)"
label variable Xp170Xo_100 "Choose which one prefer more: idealX70==1 (left) or idealX70p1 and 100 cents==2 (right)"
label variable Xp270X_100 "Choose which one prefer more: idealX70 and 100 cents==1 (left) or idealX70p2==2 (right)"
label variable Xp270X_050 "Choose which one prefer more: idealX70 and 50 cents==1 (left) or idealX70p2==2 (right)"
label variable Xp270X_025 "Choose which one prefer more: idealX70 and 25 cents==1 (left) or idealX70p2==2 (right)"
label variable Xp270 "Choose which one prefer more: idealX70==1 (left) or idealX70p2==2 (right)"
label variable Xp270Xo_025 "Choose which one prefer more: idealX70==1 (left) or idealX70p2 and 25 cents==2 (right)"
label variable Xp270Xo_050 "Choose which one prefer more: idealX70==1 (left) or idealX70p2 and 50 cents==2 (right)"
label variable Xp270Xo_100 "Choose which one prefer more: idealX70==1 (left) or idealX70p2 and 100 cents==2 (right)"


tostring sessionfile-Xp270Xo_100, replace

local varlistostring probBDM idealX90 idealX90m1 idealX90m2 idealX90p1 idealX90p2 ///
		Xm190X_100 Xm190X_050 Xm190X_025 Xm190 Xm190Xo_025 Xm190Xo_050 ///
		Xm190Xo_100 Xm290X_100 Xm290X_050 Xm290X_025 Xm290 Xm290Xo_025 ///
		Xm290Xo_050 Xm290Xo_100 Xp190X_100 Xp190X_050 Xp190X_025 Xp190  ///
		Xp190Xo_025 Xp190Xo_050 Xp190Xo_100 Xp290X_100 Xp290X_050 Xp290X_025  ///
		Xp290 Xp290Xo_025 Xp290Xo_050 Xp290Xo_100 idealX70 idealX70m1  ///
		idealX70m2 idealX70p1 idealX70p2 Xm170X_100 Xm170X_050 Xm170X_025  ///
		Xm170 Xm170Xo_025 Xm170Xo_050 Xm170Xo_100 Xm270X_100 Xm270X_050 ///
		Xm270X_025 Xm270 Xm270Xo_025 Xm270Xo_050 Xm270Xo_100 Xp170X_100 Xp170X_050 ///
		Xp170X_025 Xp170 Xp170Xo_025 Xp170Xo_050 Xp170Xo_100 Xp270X_100 Xp270X_050 ///
		Xp270X_025 Xp270 Xp270Xo_025 Xp270Xo_050 Xp270Xo_100

foreach varname in `varlistostring' {	
		replace `varname' = "" if `varname' == "-"
		destring `varname', replace
}


destring sessionfile-Xp270Xo_100, replace



save "$out_dir/`excelfile'_part2", replace 
clear 
}





/****************************************/
/*				APPEND DATA 			*/
/****************************************/

cd "$out_dir"

*Append data
use 120509_1658_NK_part2 , replace
append using 120509_1855_BK_part2
append using 120514_1712_NK_part2
append using 120516_1646_SK_part2
append using 120522_1938_CI_part2
append using 120524_1736_BK_part2
append using 120529_1820_SK_part2
append using 120604_1909_CI_part2
append using 120614_1120_part2
append using 120614_1248_part2
append using 120614_1356_part2
append using 120615_1109_part2
append using 120615_1234_part2
append using 120615_1419_part2
append using 120618_1149_part2
append using 120618_1350_part2
append using 120619_1130_part2
append using 120619_1343_part2
append using 120620_1136_part2
append using 120620_1345_part2

save fairness_part2, replace

/****************************************/
/*				CLEAN DATA	 			*/
/****************************************/
use fairness_part2, replace

*assign session number
gen session = .
	replace session= 1  if sessionfile == "120509_1658"
	replace session= 2  if sessionfile == "120509_1855"
	replace session= 3  if sessionfile == "120514_1712"
	replace session= 4  if sessionfile == "120516_1646"
	replace session= 5  if sessionfile == "120522_1938"
	replace session= 6  if sessionfile == "120524_1736"
	replace session= 7  if sessionfile == "120529_1820"
	replace session= 8  if sessionfile == "120604_1909"
	replace session= 9  if sessionfile == "120614_1120"
	replace session= 10  if sessionfile == "120614_1248"
	replace session= 11  if sessionfile == "120614_1356"
	replace session= 12  if sessionfile == "120615_1109"
	replace session= 13  if sessionfile == "120615_1234"
	replace session= 14  if sessionfile == "120615_1419"
	replace session= 15  if sessionfile == "120618_1149"
	replace session= 16  if sessionfile == "120618_1350"
	replace session= 17  if sessionfile == "120619_1130"
	replace session= 18  if sessionfile == "120619_1343"
	replace session= 19  if sessionfile == "120620_1136"
	replace session= 20  if sessionfile == "120620_1345"
label variable session "Session number"

*assign treatment number (based on global datatype) - see codebook
gen treatment = .	
	replace treatment= 1  if sessionfile == "120509_1658"
	replace treatment= 3  if sessionfile == "120509_1855"
	replace treatment= 1  if sessionfile == "120514_1712"
	replace treatment= 2  if sessionfile == "120516_1646"
	replace treatment= 4  if sessionfile == "120522_1938"
	replace treatment= 3  if sessionfile == "120524_1736"
	replace treatment= 2  if sessionfile == "120529_1820"
	replace treatment= 4  if sessionfile == "120604_1909"
	replace treatment= 4  if sessionfile == "120614_1120"
	replace treatment= 1  if sessionfile == "120614_1248"
	replace treatment= 3  if sessionfile == "120614_1356"
	replace treatment= 1  if sessionfile == "120615_1109"
	replace treatment= 2  if sessionfile == "120615_1234"
	replace treatment= 4  if sessionfile == "120615_1419"
	replace treatment= 2  if sessionfile == "120618_1149"
	replace treatment= 3  if sessionfile == "120618_1350"
	replace treatment= 3  if sessionfile == "120619_1130"
	replace treatment= 1  if sessionfile == "120619_1343"
	replace treatment= 4  if sessionfile == "120620_1136"
	replace treatment= 2  if sessionfile == "120620_1345"
label variable treatment "Treament: 1==NK; 2==SK; 3==BK; 4==CI"

*generate subject id based on session and subject number	
gen subj_id = session*100 + subjectnum
label variable subj_id "Subject ID"
order subj_id

*generate dummy for buyer
gen buyer=0
	replace buyer=1 if role==1
label variable buyer "Buyer==1, otherwise 0"

local varlistostring idealX90 idealX90m1 idealX90m2 idealX90p1 idealX90p2 ///
Xm190X_200 Xm190X_150 Xm190X_100 Xm190X_075 Xm190X_050 Xm190X_025 ///
Xm190 Xm190Xo_025 Xm190Xo_050 Xm190Xo_075 Xm190Xo_100 Xm190Xo_150 ///
Xm190Xo_200 Xm290X_200 Xm290X_150 Xm290X_100 Xm290X_075 Xm290X_050 ///
Xm290X_025 Xm290 Xm290Xo_025 Xm290Xo_050 Xm290Xo_075 Xm290Xo_100 /// 
Xm290Xo_150 Xm290Xo_200 Xp190X_200 Xp190X_150 Xp190X_100 Xp190X_075 /// 
Xp190X_050 Xp190X_025 Xp190 Xp190Xo_025 Xp190Xo_050 Xp190Xo_075 /// 
Xp190Xo_100 Xp190Xo_150 Xp190Xo_200 Xp290X_200 Xp290X_150 Xp290X_100 /// 
Xp290X_075 Xp290X_050 Xp290X_025 Xp290 Xp290Xo_025 Xp290Xo_050 /// 
Xp290Xo_075 Xp290Xo_100 Xp290Xo_150 Xp290Xo_200 ///
idealX70 idealX70m1 idealX70m2 idealX70p1 idealX70p2 /// 
Xm170X_200 Xm170X_150 Xm170X_100 Xm170X_075 Xm170X_050 Xm170X_025 /// 
Xm170 Xm170Xo_025 Xm170Xo_050 Xm170Xo_075 Xm170Xo_100 Xm170Xo_150 /// 
Xm170Xo_200 Xm270X_200 Xm270X_150 Xm270X_100 Xm270X_075 Xm270X_050 /// 
Xm270X_025 Xm270 Xm270Xo_025 Xm270Xo_050 Xm270Xo_075 Xm270Xo_100 /// 
Xm270Xo_150 Xm270Xo_200 Xp170X_200 Xp170X_150 Xp170X_100 Xp170X_075 /// 
Xp170X_050 Xp170X_025 Xp170 Xp170Xo_025 Xp170Xo_050 Xp170Xo_075 ///
Xp170Xo_100 Xp170Xo_150 Xp170Xo_200 Xp270X_200 Xp270X_150 Xp270X_100 /// 
Xp270X_075 Xp270X_050 Xp270X_025 Xp270 Xp270Xo_025 Xp270Xo_050 /// 
Xp270Xo_075 Xp270Xo_100 Xp270Xo_150 Xp270Xo_200 ///

foreach varname in `varlistostring' {
	replace `varname'=. if buyer!=0 & treatment!=2 | buyer!=0 & treatment!=4  
}

local varlistostring idealX70 idealX70m1 idealX70m2 idealX70p1 idealX70p2 /// 

foreach varname in `varlistostring' {
	replace `varname'=. if buyer==0 & treatment==2 & period<6 | buyer==0 & treatment==4 & period<6  
}


save fairness_part2, replace


*End Code



